require "util"
require "logUtil"

FuBen = {}
-- 中午执行状态
FuBen.zhongWu = false
FuBen.zhongWuHour = { 12, 13 }
--晚上执行状态
FuBen.wanShang = false
FuBen.wanShangHour = { 20 }



FuBen.ui = function()

    x, y = findMultiColorInRegionFuzzy(0xf1531a, "-49|-18|0xfef2c9,-55|-24|0x310a00,-62|-12|0xfeefba,-60|9|0xfee282,-55|0|0x310a00,-90|-2|0xfee99e,-95|3|0x310a00,-89|9|0xfee385,-107|-23|0xfef6d7", 90, 794, 3, 1078, 155, 0, 0)
    if x > -1 then
        logger("副本.界面", LOG_DEBUG)
        return true;
    end
end
FuBen.close = function()

    x, y = findMultiColorInRegionFuzzy(0xf1531a, "-49|-18|0xfef2c9,-55|-24|0x310a00,-62|-12|0xfeefba,-60|9|0xfee282,-55|0|0x310a00,-90|-2|0xfee99e,-95|3|0x310a00,-89|9|0xfee385,-107|-23|0xfef6d7", 90, 794, 3, 1078, 155, 0, 0)
    if x > -1 then
        click(x, y)
        logger("副本.退出", LOG_DEBUG)
        return true;
    end
end
FuBen.jinRuZhanChang = function()
    x, y = findMultiColorInRegionFuzzy(0xfddf17, "-10|-10|0xfef12f,1|-12|0xfef307,-11|3|0xfbd30c,-4|53|0xfeed02,22|29|0xa81f00,19|-10|0x6c1503,16|52|0x9d1c00", 90, 315, 737, 1061, 1828, 0, 0)
    if x > -1 then
        click(x, y)
        logger("副本.进入战场", LOG_DEBUG)
        return true;
    end
end




FuBen.weiJiaoChengGong = function()
    x, y = findMultiColorInRegionFuzzy(0xfef783, "-36|-6|0xfef56c,44|-4|0xfef671,122|8|0xfef788,125|-34|0xfef239,180|-2|0xfef674,222|-2|0xfef674,288|30|0xfef9b0,343|-4|0xfef679,402|8|0xfef788,362|46|0xfefbcb,58|477|0x511601,202|477|0x531701,342|479|0x521500", 90, 43, 460, 1046, 1207, 0, 0)
    if x > -1 then
        FuBen.close()
        sysLog("围剿成功")
        return true;
    end
end



FuBen.kaiZhan = function()
    x, y = findMultiColorInRegionFuzzy(0xfef00b, "-18|6|0xfeef20,-12|37|0x860000,18|15|0xa20000,55|28|0xf9ca00,35|34|0xf9c600,94|38|0xf7bb00,137|-13|0xfefefe,183|-13|0xfcfcfc,174|24|0xc2c2c2,113|3|0x8b0000,45|1|0xfef000,94|39|0xf7ba00", 85, 319, 875, 766, 1071, 0, 0)

    if x == -1 then
        x, y = findMultiColorInRegionFuzzy(0xfef65a, "19|1|0xfef21b,22|23|0xfef103,19|60|0xf8c201,-7|63|0x770000,35|46|0x9f0000,52|60|0xf8c413,71|57|0xf9cc09,101|49|0xfce743,192|28|0xc1c1c1,169|45|0xf8f9f8,139|28|0x890000,167|12|0xe9e2af", 80, 313, 815, 795, 995, 0, 0)
    end

    if x == -1 then
        x, y = findMultiColorInRegionFuzzy(0xfeef04, "14|17|0xa20000,82|22|0xfbdb13,154|16|0xfafbfb,173|22|0xc2c2c2,113|3|0x890000,-26|42|0x700000,-19|8|0xfeeb03,108|8|0x950000,52|31|0xf9cb03", 85, 315, 815, 736, 979, 0, 1)
    end

    if x > -1 then
        click(x, y)
        logger("副本.开战", LOG_DEBUG)
        return true;
    end
end


FuBen.zhanBai = function()
    x, y = findMultiColorInRegionFuzzy(0x1b3447, "6|-23|0x1d4560,46|5|0x1c3040,95|-27|0x20587a,161|68|0x647f94,-42|79|0xb1c0ca,-71|37|0x4e6477,-86|-3|0x26323c,-175|66|0x91a5b4,-153|-5|0x25455e,199|-46|0xd8dcdc,224|-62|0xe9eeee", 90, 256, 550, 882, 837, 0, 0)
    if x > -1 then
        click(x, y)
        logger("副本.战败", LOG_DEBUG)
        return true;
    end
end

FuBen.zhanSheng = function()

    x, y = findMultiColorInRegionFuzzy(0xfbfb84, "-84|71|0xf5ef13,20|39|0xdf3604,47|88|0xf9f54b,102|84|0xeae504,149|25|0xfcbe02,148|74|0xf5e206,209|85|0xf7f427,215|-14|0xf89903,229|13|0x2b0c00,236|-33|0x2b0c00,245|53|0x2b0c00,77|112|0x2b0c00,7|86|0x2b0c00,121|87|0x2b0c00,-19|66|0x2b0c00", 90, 303, 510, 799, 780, 0, 0)

    if x == -1 then
        x, y = findMultiColorInRegionFuzzy(0xfdfc4d, "47|88|0xfbfa68,8|79|0x2b0c00,-22|56|0x2b0c00,-50|64|0xf6ec07,-88|65|0xf5ea0e,101|76|0xece907,210|85|0xf7f33a,209|60|0xf6e905,232|61|0x2b0c00,225|10|0x2b0c00,219|-13|0xf18804,243|-5|0x2b0c00,131|4|0x2b0c00,111|10|0xfc9702,42|27|0x2b0c00,59|68|0x2b0c00,-65|-4|0xf86d03", 85, 256, 402, 867, 842, 0, 0)
    end

    if  x==-1 then
        x, y = findMultiColorInRegionFuzzy(0xfef903,"-1|44|0xfef805,-1|126|0xfbf973,-72|86|0x0e0a08,-117|106|0xf7f136,-250|82|0xefd709,-226|12|0xf96c00,-180|61|0x2b0d00,-229|99|0x2b0d00,-158|102|0x2b0d00,-105|87|0x280b00,-146|59|0xe42f00,-163|14|0xfefe09,1|3|0xfef804,-2|48|0xfefa0c,16|91|0x2b0d00,-2|127|0xfbf972,14|135|0x2b0d00", 95, 306, 561, 754, 848, 0, 0)
    end
    if x > -1 then
        click(x, y)
        logger("副本.战胜", LOG_DEBUG)
        return true;
    end
end

FuBen.xuanZheMenKe = function()
    x, y = findMultiColorInRegionFuzzy(0xffe958, "0|-8|0xffe958,-38|-3|0xffe958,42|-9|0xffe958,63|-7|0xffe958,63|3|0xffe958,79|5|0xffe958,85|-11|0xffe958,85|-11|0xffe958,-36|1|0x1a1912,113|-3|0x1a1812", 90, 414, 1262, 728, 1442, 0, 0)

    if x == -1 then
        point = findColors({ 446, 1307, 689, 1365 },
        {
            { x = 511, y = 1336, color = 0xffe958 }
        },
        99, 0, 0, 0)

        if #point > 200 then
            x, y = point[200].x, point[200].y
        end
    end

    if x==-1 then
        x, y = findMultiColorInRegionFuzzy(0x171711,"-14|46|0x171611,-44|188|0x191914,5|247|0x171611,42|247|0x181611,-99|160|0x191914,60|127|0x191912,81|90|0x1b1912,-170|224|0xe3caab,-151|205|0xb29376,189|173|0xab8d67,48|227|0x212019,11|227|0x201f16,-46|272|0x5f5648,-73|270|0x514a3c,86|272|0x645d49,112|268|0x4d4937,73|6|0xd6c89b,-131|-28|0xe5ceab", 95, 250, 1103, 885, 1627, 0, 0)

    end
    if x > -1 then
        logger("副本.战斗完成", LOG_DEBUG)
        return true;
    end
end

FuBen.resetState = function()
    local now = os.date("*t", os.time())

    local currentHour = now.hour;
    sysLog("当前小时：" .. tostring(currentHour))
    sysLog("中午小时：" .. tostring(FuBen.zhongWuHour[1]))

    if currentHour < FuBen.zhongWuHour[1] then
        FuBen.zhongWu = false
    end

    if currentHour < FuBen.wanShangHour[1] then
        FuBen.wanShang = false
    end
end

-- 师傅需要执行
FuBen.needRun = function()

    local now = os.date("*t", os.time())
    local currentHour = now.hour;
    local currentMinute = now.min

    --中午代码
    if not FuBen.zhongWu then
        for i, tempHour in ipairs(FuBen.zhongWuHour) do
            if tempHour == currentHour and currentMinute > 5 then
                return true
            end
        end
    end

    -- 晚上
    if not FuBen.wanShang then
        for i, tempHour in ipairs(FuBen.wanShangHour) do
            if tempHour == currentHour and currentMinute > 5 then
                return true
            end
        end
    end
end

FuBen.run = function()
    --
    if not FuBen.ui() then
        return
    end
    local flag = false;

    for i = 1, 5 do
        flag = FuBen.jinRuZhanChang()
        if flag then
            break
        end
        mSleep(300)
    end

    local funcArr = { FuBen.kaiZhan, FuBen.zhanSheng, FuBen.jinRuZhanChang, FuBen.zhanBai };
    local wcFuncArr = { FuBen.xuanZheMenKe, FuBen.weiJiaoChengGong };
    while flag do
        for i, func in ipairs(funcArr) do
            func()
            mSleep(50)
        end
        for i, func in ipairs(wcFuncArr) do
            flag = func()
            flag = not (flag)
            if flag == false then
                break
            end

        end
        mSleep(200)
    end

    for i = 1, 2 do
        FuBen.close()
        mSleep(2000)
    end

    -- 记录运行过
    local now = os.date("*t", os.time())
    local currentHour = now.hour;
    --中午代码
    if not FuBen.zhongWu then
        for i, tempHour in ipairs(FuBen.zhongWuHour) do
            if tempHour == currentHour then
                FuBen.zhongWu = true
            end
        end
    end

    -- 晚上
    if not FuBen.wanShang then
        for i, tempHour in ipairs(FuBen.wanShangHour) do
            if tempHour == currentHour then
                FuBen.wanShang = true
            end
        end
    end

    return true
end

